package com.wbao.dianniu.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.listener.IActivityDelListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.ActivityDelManager;
import com.wbao.dianniu.manager.ReleaseDataManager;
import com.wbao.dianniu.manager.ShareSDKManager;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 活动管理
 * 
 * @author yejianjun
 * 
 */
public class ActManagerActivity extends BaseActivity implements IActivityDelListener {

	private Button rightBtn;
	private TextView tv_browse;
	private TextView tv_signup;
	private TextView tv_pass;
	private Button btn_registration;
	private Button btn_modify;
	private Button btn_delete;
	private Button btn_share;
	private int id;
	private String address;
	private String price;
	private String subject;
	private String maxPersion;
	private String startDate;
//	private String content;
	private ActivityDelManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_act_manager,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.act_manager));
		init();
		initData();
		manager = new ActivityDelManager(this);
		manager.addActivityDelListener(this);
		
	}

	private void init() {
		rightBtn = getRightTextButton();
		rightBtn.setText("查看详情");
		rightBtn.setOnClickListener(this);
		
		tv_browse = (TextView) findViewById(R.id.browse_tv);
		tv_signup = (TextView) findViewById(R.id.sign_up_tv);
		tv_pass = (TextView) findViewById(R.id.pass_tv);
		btn_registration = (Button) findViewById(R.id.registration_btn);
		btn_modify = (Button) findViewById(R.id.modify_act_btn);
		btn_delete = (Button) findViewById(R.id.delete_act_btn);
		btn_share = (Button) findViewById(R.id.share_act_btn);
		btn_registration.setOnClickListener(this);
		btn_modify.setOnClickListener(this);
		btn_delete.setOnClickListener(this);
		btn_share.setOnClickListener(this);
		
	}

	private void initData(){
		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra(GlobalContext.BUNDLE_DATA);
		if(null != bundle){
			tv_browse.setText(bundle.getString(GlobalContext.BUNDLE_BROWSE, ""));
			tv_signup.setText(bundle.getString(GlobalContext.BUNDLE_SIGNUP, ""));
			tv_pass.setText(bundle.getString(GlobalContext.BUNDLE_PASS, ""));
			id = bundle.getInt(GlobalContext.BUNDLE_ID, 0);
			address = bundle.getString(GlobalContext.BUNDLE_ADDRESS, "");
			price = bundle.getString(GlobalContext.BUNDLE_PRICE, "");
			subject = bundle.getString(GlobalContext.BUNDLE_SUBJECT, "");
			maxPersion = bundle.getString(GlobalContext.BUNDLE_MAXPERSION, "");
			startDate = bundle.getString(GlobalContext.BUNDLE_STARTDATE, "");
//			content = bundle.getString(GlobalContext.BUNDLE_CONTENT, "");
		}
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.registration_btn:
			Intent intent = new Intent();
			intent.putExtra(GlobalContext.BUNDLE_ID, id);
			intent.setClass(this, SignUpManagerActivity.class);
			startActivity(intent);
			break;
		case R.id.modify_act_btn:
			Intent modifyIntent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putInt(GlobalContext.BUNDLE_ID, id);
			bundle.putString(GlobalContext.BUNDLE_ADDRESS, address);
			bundle.putString(GlobalContext.BUNDLE_PRICE, price);
			bundle.putString(GlobalContext.BUNDLE_SUBJECT, subject);
			bundle.putString(GlobalContext.BUNDLE_MAXPERSION, maxPersion + "人");
			bundle.putString(GlobalContext.BUNDLE_STARTDATE, startDate);
//			bundle.putString(GlobalContext.BUNDLE_CONTENT, content);
			modifyIntent.putExtra(GlobalContext.BUNDLE_EDIT, bundle);
			modifyIntent.setClass(this, CreateActivity.class);
			startActivity(modifyIntent);
			break;
		case R.id.delete_act_btn:
			CustomDialog.Builder builder = new CustomDialog.Builder(this);
			builder.setTitle("确认删除该条活动吗？");
			builder.setPositiveButton(getResources().getString(R.string.confirm),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							manager.activityDel(GlobalContext.accountId, id);
						}
					});
			builder.setNegativeButton(getResources().getString(R.string.cancel),
					new android.content.DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.create().show();
			break;
		case R.id.share_act_btn:
			// 调用分享内容
			String imageUrl = GlobalContext.cdndownUrl + "logo.png";
			String content = "活动主题：" +subject + "\n" + "活动时间：" +startDate + "\n" + "活动地点：" + address;
			ShareSDKManager.getInstance().showShare(this, content, 6, imageUrl, id);
			break;
		case R.id.back_button:
			finish();
			break;
		case R.id.right_button:
			Intent rightIntent = new Intent();
			rightIntent.putExtra(GlobalContext.INTENT_ACTIVITYID, id);
			rightIntent.setClass(this, ActivityDetail.class);
			startActivity(rightIntent);
			break;
		default:
			break;
		}
		
	}

	@Override
	public void onActivityDelFailure(int errorCode, String errorMsg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onActivityDelSuccess() {
		// TODO Auto-generated method stub
		Notification.toast(this, "活动已删除");
		//删除该条记录
		ReleaseDataManager.getInstance().delete(id);
		finish();
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
